const app = getApp()
import util from '../../utils/util'
import {
    GetGoodsStore,
    WxPayTrade
} from '../../config/api'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        model: '',
        testarr: [1, 1, 1, 1, 1, 1, 1, 1, 1], //测试循环数组
        goodsList: [], //农产品商品列表数据
        flag: false, //是否显示遮罩，默认不显示
        num: 1, //购买数量，默认为1
        price: 0, //每件物品价格，默认为0
        img: '', //选中的物品图片
        goodsname: '', //单个物品名称
        id: '', //单个物品名称id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let model = wx.getStorageSync('model')
        this.setData({
            model
        });

        let that = this;
        util.apiRequest(GetGoodsStore, {
            page: 1,
            limit: 100,
            vollege_id: app.globalData.vollege_id,
        }, null).then(r => {
            console.log('农产品商城：', r.data.data.data);
            that.setData({
                goodsList: r.data.data.data,
            });
        });
    },


    /**
     * 购买
     */
    BuyNow: function () {
        console.log('您点击了购买！');
        // 跳转到购买页面
        wx.navigateTo({
            url: '/pages/goods/buygoods/buygoods?num=' + this.data.num + '&price=' + this.data.price + '&img=' + this.data.img + '&name=' + this.data.goodsname + '&id=' + this.data.id,
        });

    },
    /**
     * 增加
     */
    Add: function () {
        let that = this;
        console.log('您点击了增加！', that.data.num);
        if (that.data.num >= 0) {
            that.setData({
                num: that.data.num + 1
            });
        }
    },
    /**
     * 减少
     */
    Reduce: function () {
        let that = this;
        console.log('您点击了减少！', that.data.num);
        if (that.data.num <= 1) {
            that.setData({
                num: 1
            });
        }
        // else if(that.data.num==0){
        //     that.setData({
        //         num:0
        //     });
        // }
        else {
            that.setData({
                num: that.data.num - 1
            });
        }
    },

    /**
     * 支付调用
     */
    //取消遮罩
    cancelZhezao: function (e) {
        console.log('取消遮罩');
        let that = this;
        that.setData({
            flag: false,
            num: 1,
            price: 0,
            goodsname: ''
        });
    },

    //现在的逻辑
    DuiHuan: function (e) {

        //现在的逻辑跳转到商品详情页面
        wx.navigateTo({
            url: '/pages/goods/goods_detail/goods_detail?id=' + e.currentTarget.dataset['id'],
        });
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})